package com.noahplat.person.service.impl;

import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.FirstDepartMapper;
import com.noahplat.person.mapper.MedicalRecordMapper;
import com.noahplat.person.mapper.SecondDepartMapper;
import com.noahplat.person.pojo.FirstDepart;
import com.noahplat.person.pojo.MedicalRecord;
import com.noahplat.person.pojo.SecondDepart;
import com.noahplat.person.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: MedicalRecordServiceImpl
 * @Description: 就诊记录实现类
 * @Author 李景星
 * @Date 2021/3/6
 * @Version 1.0
 */
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;
    @Autowired
    private FirstDepartMapper firstDepartMapper;
    @Autowired
    private SecondDepartMapper secondDepartMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 增加就诊记录
     * @param medicalRecord
     * @return
     */
    @Override
    public Result addMedicalRecord(MedicalRecord medicalRecord) {
        int res = medicalRecordMapper.insert(medicalRecord);
        if(res > 0){
            return new Result(true, StatusCode.OK, "新增就诊记录成功");
        }
        return new Result(false, StatusCode.ERROR, "新增就诊记录失败");
    }

    /**
     * 更新就诊记录
     * @param medicalRecord
     * @return
     */
    @Override
    public Result updateMedicalRecord(MedicalRecord medicalRecord) {
        int res = medicalRecordMapper.updateByPrimaryKeySelective(medicalRecord);
        if(res > 0){
            return new Result(true, StatusCode.OK, "更新就诊记录成功");
        }
        return new Result(false, StatusCode.ERROR, "更新就诊记录失败");
    }

    /**
     * 查询就诊记录详情
     * @param id
     * @return
     */
    @Override
    public Result getMedicalRecordInfo(Long id) {
        Example example = new Example(MedicalRecord.class);
        example.createCriteria().andEqualTo("id", id).andEqualTo("del", "N");
        MedicalRecord medicalRecord = medicalRecordMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(medicalRecord)){
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
        //封装一级科室名称
        if(redisUtils.hasKey(NoahEnum.FIRSTDEPARTDIR.getCode()+NoahEnum.FIRSTDEPARTKEY.getCode())){
            List<FirstDepart> list = (List<FirstDepart>)(List)redisUtils.lGet(NoahEnum.FIRSTDEPARTDIR.getCode() + NoahEnum.FIRSTDEPARTKEY.getCode(), 0, -1);
            for (FirstDepart firstDepart : list) {
                if(firstDepart.getId() == medicalRecord.getFdepart()){
                    medicalRecord.setFdepartName(firstDepart.getFdepart());
                }
            }
        }else {
            FirstDepart firstDepart = firstDepartMapper.selectByPrimaryKey(medicalRecord.getFdepart());
            medicalRecord.setFdepartName(firstDepart.getFdepart());
        }
        //封装二级科室名称
        if(redisUtils.hasKey(NoahEnum.SECONDDEPARTDIR.getCode()+NoahEnum.SECONDDEPARTKEY.getCode())){
            List<SecondDepart> list = (List<SecondDepart>)(List)redisUtils.lGet(NoahEnum.SECONDDEPARTDIR.getCode()+NoahEnum.SECONDDEPARTKEY.getCode(), 0, -1);
            for (SecondDepart secondDepart : list) {
                if(secondDepart.getId() == medicalRecord.getSdepart()){
                    medicalRecord.setSdepartName(secondDepart.getSdepart());
                }
            }
        }else {
            SecondDepart secondDepart = secondDepartMapper.selectByPrimaryKey(medicalRecord.getSdepart());
            medicalRecord.setSdepartName(secondDepart.getSdepart());
        }
        return new Result(true, StatusCode.OK, "查询成功", medicalRecord);
    }

}
